Imports Aricie.Services

Namespace Aricie.DNN.Caching.Client
    Public Class CacheCallBackAdapter

        Private _ItemRemovedCallBack As CacheItemRemovedCallback

        Public Sub New(ByVal itemRemovedCallBack As CacheItemRemovedCallback)
            Me._ItemRemovedCallBack = itemRemovedCallBack
        End Sub

        'todo: deal with multiple regions
        Public Sub SetCallBack(ByVal itemRemovedCallBack As CacheItemRemovedCallback)
            If _ItemRemovedCallBack.Method IsNot itemRemovedCallBack.Method OrElse _ItemRemovedCallBack.Target IsNot itemRemovedCallBack.Target Then
                Dim newDelegate As MulticastDelegate = ReflectionHelper.CombineDelegate(_ItemRemovedCallBack, itemRemovedCallBack, True)
                _ItemRemovedCallBack = CType(newDelegate, CacheItemRemovedCallback)
            End If
        End Sub

        Public Sub OnCacheNotificationCallBack(ByVal key As String, ByVal value As Object, ByVal reason As CacheItemRemovedReason)
            Try
                Me._ItemRemovedCallBack.Invoke(key, value, reason)
            Catch ex As Exception
                Aricie.DNN.Diagnostics.AsyncLogger.Instance.AddException(ex)
            End Try
        End Sub

    End Class
End Namespace